package com.szholly.pro.watergas_server.ps.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.common.web.BaseController;
import com.szholly.pro.watergas_server.common.web.RequestUnit;
import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.PageInfo;
import com.szholly.data.general.RowBase;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/SamplingQueryController/")
public class SamplingQueryController  extends BaseController {
	/**
	 * 获取采样数据
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value="/getSamplingList")
	public void getSamplingList(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String st=RequestUnit.getParameter("START_TIME")+" 00:00:00";
	    String et=RequestUnit.getParameter("END_TIME")+" 23:59:59";
	    String MN=RequestUnit.getParameter("MN");
	    String sql="";
	    if (MN != null && !MN.equals("")) {
	    	 sql="select t3.name as STATION_NAME,t1.* from (select * from (select mn,param,PARAM_VALUE,qn,CUP_ORDER,ct from OP_SAMP_INFO where mn='"+MN+"' and ct >=to_date('"+st+"','yyyy-MM-dd hh24:mi:ss') and ct <=to_date('"+et+"','yyyy-MM-dd hh24:mi:ss')) pivot(avg(PARAM_VALUE)  for(PARAM) in ('101' TP,'060' NH4,'001' PH,'011' COD,'ss' SS)))t1"
		    		+ " left join BC_COLLECTION_INFO t2 on t1.mn=t2.device_num"
		    		+ " left join BC_SITE_INFO t3 on t2.site_id=t3.exp_no";
	    }
	    else{
	    	sql="select t3.name as STATION_NAME,t1.* from (select * from (select mn,param,PARAM_VALUE,qn,CUP_ORDER,ct from OP_SAMP_INFO where  ct >=to_date('"+st+"','yyyy-MM-dd hh24:mi:ss') and ct <=to_date('"+et+"','yyyy-MM-dd hh24:mi:ss')) pivot(avg(PARAM_VALUE)  for(PARAM) in ('101' TP,'060' NH4,'001' PH,'011' COD,'ss' SS)))t1"
		    		+ " left join BC_COLLECTION_INFO t2 on t1.mn=t2.device_num"
		    		+ " left join BC_SITE_INFO t3 on t2.site_id=t3.exp_no";
	    }
	   sql+=" order by ct desc";
	    PageInfo pageInfo = DataUtil.getPageInfo();
	    List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
	    List<RowBase> list =DataUtil.getPageRow(sql, parms, null, pageInfo);
	    JSONObject json=DataUtil.listToJSONObject(list,null,pageInfo);
		outString(json.toString());
	}
}
